package chapter_18.demo_1;

/**
 * Created by Administrator on 2018/8/7.
 * 匿名内部类(非 Lambda表达式 时的形式)
 */
public class CalculableMainTest {

    public static Calculable calculable(char opr) {

        Calculable calculable;

        if (opr == '+') {
            // 匿名内部类实现 Calculable 接口
            calculable = new Calculable() {
                @Override
                public int calculateInt(int a, int b) {
                    return a + b;
                }
            };
        } else {
            // 匿名内部类实现 Calculable 接口
            calculable = new Calculable() {
                @Override
                public int calculateInt(int a, int b) {
                    return a - b;
                }
            };
        }

        return calculable;
    }

    public static void main(String[] args) {

        int n1 = 10;
        int n2 = 5;

        // 实现加法计算 Calculable 对象
        Calculable calculable1 = calculable('+');
        System.out.printf("%d + %d = %d \n", n1 , n2 , calculable1.calculateInt(n1, n2));
        // 实现减法计算 Calculable 对象
        Calculable calculable2 = calculable('-');
        System.out.printf("%d - %d = %d \n", n1 , n2 , calculable2.calculateInt(n1, n2));
    }
}
